using UnityEngine;

public class CameraRoot : MonoBehaviour
{
    [SerializeField]private float panSpeed;//平移移动速度
    [SerializeField] private float panMoveTime;//平移缓冲时间

    public float maxLeft;
    public float maxRight;
    public float maxDown;
    public float maxTop;
    
    private Vector3 newPos;

    // Start is called before the first frame update
    void Start()
    {
        newPos = transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        MouseInput();
    }
    
    //在边缘时的移动
    void MouseInput()
    {
        Vector2 mousePos = Input.mousePosition;
        if(mousePos.x>Screen.width*0.96 && mousePos.x < Screen.width)
        {
            newPos -= Vector3.forward * panSpeed * Time.unscaledDeltaTime * (float)((mousePos.x - Screen.width * 0.96) / (Screen.width*0.04));
        }
        else if (mousePos.x < Screen.width * 0.02 && mousePos.x >= 0)
        {
            newPos += Vector3.forward * panSpeed * Time.unscaledDeltaTime * (float)((Screen.width * 0.04-mousePos.x) / (Screen.width * 0.04));
        }
        else if(mousePos.y > Screen.height * 0.95 && mousePos.y < Screen.height)
        {
            newPos += Vector3.right * panSpeed * Time.unscaledDeltaTime * (float)((mousePos.y - Screen.height * 0.95) / (Screen.height * 0.05));
        }
        else if(mousePos.y < Screen.height * 0.03 && mousePos.y >= 0)
        {
            newPos -= Vector3.right * panSpeed * Time.unscaledDeltaTime * (float)((Screen.height * 0.05-mousePos.y ) / (Screen.height * 0.05));
        }
        newPos.x = Mathf.Clamp(newPos.x, maxDown, maxTop);
        newPos.z = Mathf.Clamp(newPos.z, maxLeft, maxRight);


        transform.position = Vector3.Lerp(transform.position, newPos, panMoveTime * Time.unscaledDeltaTime);
    }
}
